﻿using System;
using AEM.Data.BLL.Users;
using AEM.Utils;

namespace AEM.Data.BLL.Actions
{
    /// <summary>
    /// 处理用户相关
    /// 与UI交互的类
    /// LYNX.KOR
    /// 2011-10-20
    /// </summary>
    public static class User
    {
        static User()
        {
            UserLogin = doUserLogin;
            Adduser = doAdduser;
            Modifiyuser = doModifiyuser;
            Deleteuser = doDeleteuser;
            ModifiyPassword = doModifiyPpassword;
            ResetPassword = doResetPassword;


            IsFirstLogin = doIsFirstLogin;
        }

        #region 调用的函数
        private static UserLoginResult doUserLogin(string username, string pwd)
        {
            Model.User model = DAL.User.Select("UserName", username);
            if (null == model) return UserLoginResult.UserNameIsNotExist;

            if (model.UserPWD != pwd.toMD5()) return UserLoginResult.UserPWDIsWrong;

            Config.userLogin = new UserLoginSuccessArgs();
            Config.userLogin.ID = model.ID;
            Config.userLogin.UserName = model.UserName;
            Config.userLogin.UserPWD = model.UserPWD;
            Config.userLogin.UserGroup = model.UserGroup;
            Config.userLogin.UserPermission = model.UserPermission;
            Config.userLogin.CreateDate = model.CreateDate;
            Config.userLogin.LastUpdatePwdDate = model.LastUpdatePwdDate;
            Config.userLogin.LastLoginDate = model.LastLoginDate = DateTime.Now; ;

            DAL.User.Update(model);

            return UserLoginResult.OK;
        }

        private static AdduserResult doAdduser(Model.User value)
        {
            if (null == value) return AdduserResult.UserIsNull;
            Model.User model = DAL.User.Select("UserName", value.UserName);
            if (null != model) return AdduserResult.UserAlreadyExist;

            bool ret = DAL.User.Add(value);
            if (ret)
                return AdduserResult.OK;
            else
                return AdduserResult.Error;
        }

        private static ModifiyUserResult doModifiyuser(Model.User value)
        {
            if (null == value) return ModifiyUserResult.UserIsNull;
            Model.User model = DAL.User.Select("ID", value.ID.ToString());
            if (null == model) return ModifiyUserResult.UserIsNotExist;

            bool ret = DAL.User.Update(value);
            if (ret)
                return ModifiyUserResult.OK;
            else 
                return ModifiyUserResult.Error;
        }

        private static DeleteuserResult doDeleteuser(int userid)
        {
            Model.User model = DAL.User.Select("ID", userid.ToString());
            if (null==model) return DeleteuserResult.UserIsNotExist;

            bool ret = DAL.User.Delete(userid);
            if (ret)
                return DeleteuserResult.OK;
            else
                return DeleteuserResult.Error;
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="oldpwd"></param>
        /// <param name="newpwd"></param>
        /// <returns></returns>
        private static ModifiyUserResult doModifiyPpassword(string oldpwd, string newpwd)
        {
            Model.User model = DAL.User.Select("ID", Config.userLogin.ID.ToString());
            if (null==model) return ModifiyUserResult.UserIsNotExist;
            if (model.UserPWD != oldpwd.toMD5()) return ModifiyUserResult.WrongPassword;
            model.UserPWD = newpwd.toMD5();
            model.LastUpdatePwdDate = DateTime.Now;
            bool ret = DAL.User.Update(model);
            if (ret)
                return ModifiyUserResult.OK;
            else
                return ModifiyUserResult.Error;
        }

        private static ModifiyUserResult doResetPassword(string value)
        {
            if (value.ToUpper() == "ADMIN") return ModifiyUserResult.UserIsAdmin;
            Model.User model = DAL.User.Select("UserName", value);
            if (null == model) return ModifiyUserResult.UserIsNotExist;
            model.UserPWD = "123456".toMD5();
            bool ret = DAL.User.Update(model);
            if (ret)
                return ModifiyUserResult.OK;
            else
                return ModifiyUserResult.Error;
        }


        private static bool doIsFirstLogin()
        {
            if (null != Config.userLogin.LastUpdatePwdDate && Config.userLogin.CreateDate < Config.userLogin.LastUpdatePwdDate)
            {
                return false;
            }
            return true;
        }
        #endregion 函数end

        #region 委托群

        public static Func<string, string, UserLoginResult> UserLogin;

        /// <summary>
        /// 添加用户
        /// 第一个参数，添加用户需要的数据
        /// 返回值:添加成功后返回AdduserResult
        /// 异常：UnauthorizedAccessException和Exception
        /// </summary>
        public static Func<Model.User, AdduserResult> Adduser;

        /// <summary>
        /// 修改用户
        /// 第一个参数，修改用户需要的数据
        /// 返回值:修改成功后返回ModifiyuserResult
        /// </summary>
        public static Func<Model.User, ModifiyUserResult> Modifiyuser;

        /// <summary>
        /// 删除用户
        /// 每一个参数：用户ID
        /// 返回值：操作成功后返回DeleteuserResult
        /// </summary>
        public static Func<int, DeleteuserResult> Deleteuser;

        /// <summary>
        /// 用户自己修改密码
        /// 第一个参数：旧密码
        /// 第二个参数：新密码
        /// 返回值：操作成功后返回ModifiyUserResult
        /// </summary>
        public static Func<string, string, ModifiyUserResult> ModifiyPassword;

        /// <summary>
        /// 用户退出登录
        /// </summary>
        public static Action LoginOut;

        /// <summary>
        /// 判断是否为第一次登录
        /// </summary>
        public static Func<bool> IsFirstLogin;

        public static Func<string, ModifiyUserResult> ResetPassword;
        #endregion 委托群end

        #region 枚举群

        public enum UserLoginResult
        {
            OK,
            UserNameIsNotExist,
            UserPWDIsWrong
        }

        public enum AdduserResult
        {
            OK,
            /// <summary>
            /// 传入用户信息为空
            /// </summary>
            UserIsNull,
            /// <summary>
            /// 用户已存在
            /// </summary>
            UserAlreadyExist,
            Error
        }

        public enum ModifiyUserResult
        {
            OK,
            /// <summary>
            /// 传入用户信息为空
            /// </summary>
            UserIsNull,
            /// <summary>
            /// 用户不存在
            /// </summary>
            UserIsNotExist,
            /// <summary>
            /// 密码错误
            /// </summary>
            WrongPassword,
            Error,
            UserIsAdmin
        }

        public enum DeleteuserResult
        {
            OK,
            UserIsNotExist,
            Error
        }

        #endregion 枚举群end

        public static void init() { }

    }
}
